﻿using System;
using System.Collections.Generic;
using System.IO;
using System.ComponentModel;
using System.Text;
using System.Threading;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;

namespace Scheduler
{
    public static class Extensions
    {
        public static T CreateIfNull<T>(ref T item) where T : class, new()
        {
            if (item == null) { Interlocked.CompareExchange<T>(ref item, new T(), null); }
            return item;
        }

        public static TInterface CreateIfNull<TInterface, TClass>(ref TInterface item)
            where TInterface : class
            where TClass : class, TInterface, new()
        {
            if (item == null) { Interlocked.CompareExchange<TInterface>(ref item, new TClass(), null); }
            return item;
        }

        public static Thread CreateAndRunThread(this ThreadStart ts)
        {
            Thread t = CreateThread(ts);
            t.Start();
            return t;
        }

        public static Thread CreateThread(this ThreadStart ts)
        {
            Thread t = new Thread(ts);
            t.TrySetApartmentState(ApartmentState.MTA);
            t.Priority = ThreadPriority.Normal;
            t.IsBackground = true;
            return t;
        }
    }
}
